using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace pxt
{
    public class InputSystem : Microsoft.Xna.Framework.GameComponent
    {
        // Members
        private KeyboardState kbState, prevKbState;
        private MouseState mouseState, prevMouseState;
        private List<GamePadState> mGamePadStates, mGamePadPrevStates;
        private Dictionary<int, PlayerIndex> mIntToIndex;
        private Dictionary<PlayerIndex, int> mIndexToInt;

        public InputSystem(Game game) : base(game)
        {
            // TODO: Construct any child components here
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
            kbState = new KeyboardState();
            prevKbState = new KeyboardState();
            mouseState = new MouseState();
            prevMouseState = new MouseState();
            mIntToIndex = new Dictionary<int, PlayerIndex>();
            mIntToIndex.Add(0, PlayerIndex.One);
            mIntToIndex.Add(1, PlayerIndex.Two);
            mIntToIndex.Add(2, PlayerIndex.Three);
            mIntToIndex.Add(3, PlayerIndex.Four);
            mIndexToInt = new Dictionary<PlayerIndex, int>();
            mIndexToInt.Add(PlayerIndex.One, 0);
            mIndexToInt.Add(PlayerIndex.Two, 1);
            mIndexToInt.Add(PlayerIndex.Three, 2);
            mIndexToInt.Add(PlayerIndex.Four, 3);
            mGamePadStates = new List<GamePadState>();
            mGamePadPrevStates = new List<GamePadState>();
            for (int i = 0; i < 4; i++)
            {
                mGamePadStates.Add(new GamePadState());
                mGamePadPrevStates.Add(new GamePadState());
            }
            //  TODO: Add support for more players
            

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            prevKbState = kbState;
            prevMouseState = mouseState;
            for (int i = 0; i < 4; i++)
            {
                mGamePadPrevStates[i] = mGamePadStates[i];
            }
            base.Update(gameTime);
        }
        /// <summary>
        /// Be sure to call this at the beginning of your update loop as
        /// it grabs the current state of the keyboard/mouse, and match it 
        /// with a call to Update() at the end of the of your Update()
        /// </summary>
        public void PollInputDevices()
        {
            kbState = Keyboard.GetState();
            mouseState = Mouse.GetState();
            for (int i = 0; i < 4; i++)
            {
                mGamePadStates[i] = GamePad.GetState(mIntToIndex[i]);
            }
        }

        public bool CheckKeyPress(Keys key)
        {
            return (kbState.IsKeyDown(key) && prevKbState.IsKeyUp(key));
        }

        public bool CheckKeyPressContinuous(Keys key)
        {
            return (kbState.IsKeyDown(key));
        }

        public bool CheckButtonPress(Buttons button, PlayerIndex index)
        {
            return (mGamePadStates[mIndexToInt[index]].IsButtonDown(button) &&
                    mGamePadPrevStates[mIndexToInt[index]].IsButtonUp(button));
        }

        public bool CheckButtonPressContinuous(Buttons button, PlayerIndex index)
        {
            return (mGamePadStates[mIndexToInt[index]].IsButtonDown(button));
        }

        public bool CheckLeftMouseButton()
        {
            return (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released);
        }

        #region Properties
        public MouseState MouseInfo
        {
            get { return mouseState; }
        }
        #endregion
    }
}